package cn.wolfcode.controller;

import cn.wolfcode.domain.Emp;
import cn.wolfcode.domain.Result;
import cn.wolfcode.expection.LoginException;
import cn.wolfcode.service.impl.EmpServiceImpl;
import cn.wolfcode.util.JWTUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
@Slf4j
@RestController
public class LoginController {
    @Autowired
    private EmpServiceImpl empService;
    @PostMapping("/login")
    public Result login(@RequestBody  Emp loginemp)
    {
        Emp emp=empService.login(loginemp);
        if (emp != null)
        {
            Map<String,Object> information=new HashMap<>();
            information.put("username",emp.getUsername());
            information.put("name",emp.getName());
          String token= JWTUtil.getToken(information);
          log.info("toke={}",token);
           return Result.success(token);
        }
        else
        {
            throw  new LoginException("password or username error,stupid");
        }
    }
}
